//
//  HMStatusRetweetView.swift
//  大深圳微博01
//
//  Created by shenzhenIOS on 16/4/15.
//  Copyright © 2016年 shenzhenIOS. All rights reserved.
//

import UIKit

/// 被转发的view
class HMStatusRetweetView: UIView {
    
    /// 被转发的微博
    var retweetStatus: HMStatus? {
        didSet {
            // 当别人设置被转发微博的内容,我们来设置相应的内容到view上面
            // 被转发的微博文本内容 = @被转发微博的用户名称: 被转发微博的文本内容
            let retweetName = retweetStatus?.user?.screen_name ?? "被转发微博的用户名称"
            let retweetText = retweetStatus?.text ?? "被转发微博内容为空"
            
            let retweetContent = "@\(retweetName): \(retweetText)"
            retweetLabel.text = retweetContent
            
            // 设置配图
            retweetPictureView.pictureURLs = retweetStatus?.storePicutreURLs
            
            // 当没有图片的时候,HMStatusPictureView距离retweetLabel的底部不需要8的间距
            // snp_updateConstraints: 前提是参照的view和参照的约束没有变化才能updateConstraints
            retweetPictureView.snp_updateConstraints { (make) -> Void in
                let margin = retweetStatus?.storePicutreURLs == nil ? 0 : HMStatusCellMargin
                make.top.equalTo(retweetLabel.snp_bottom).offset(margin)
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    private func prepareUI() {
        // 1.添加控件
        addSubview(bkgButton)
        addSubview(retweetLabel)
        addSubview(retweetPictureView)
        
        // 2.添加约束
        // 背景按钮
        bkgButton.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        
        // 被转发的微博文本内容
        retweetLabel.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(HMStatusCellMargin)
            make.trailing.equalTo(self).offset(-HMStatusCellMargin)
        }
        
        // 配图
        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(HMStatusCellMargin)
            make.top.equalTo(retweetLabel.snp_bottom).offset(HMStatusCellMargin)
            
            // 配图大小当别人给他设置pictureURL后他会自动更新,不用我们操心
            make.size.equalTo(CGSizeZero)
        }
        
        // 添加底部约束让被转发的view的底部参照被转发的配图视图
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(retweetPictureView).offset(HMStatusCellMargin)
        }
    }

    // MARK: - 懒加载控件
    /// 背景按钮
    private lazy var bkgButton: UIButton = { () -> UIButton in
        let button = UIButton()
        
        // 设置按钮
        button.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
        
        return button
    }()
    
    /// 被转发的文字
    private lazy var retweetLabel: UILabel = {
        let label = UILabel(fontSize: HMStatusContentFontSize, textColor: UIColor.darkGrayColor())
        
        label.numberOfLines = 0
        
        // 换行宽度
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * HMStatusCellMargin
        
        return label
    }()
    
    /// 被转发的配图视图
    private lazy var retweetPictureView: HMStatusPictureView = HMStatusPictureView()
}
